package org.budo.support.web.realscheme;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.budo.support.lang.util.StringUtil;

/**
 * @author lmw
 */
public class RealSchemeFilter implements Filter {
    private String[] realSchemeHeaderNames = new String[] { "X-Forwarded-Proto", "x-forwarded-proto", "X-Real-Scheme" };

    public void init(FilterConfig filterConfig) throws ServletException {
        String names = filterConfig.getInitParameter("real-scheme-header-names");
        if (!StringUtil.isEmpty(names)) {
            realSchemeHeaderNames = StringUtil.split(names, ",");
        }
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        RealSchemeRequestWrapper requestWrapper = new RealSchemeRequestWrapper((HttpServletRequest) request, realSchemeHeaderNames);
        chain.doFilter(requestWrapper, response);
    }

    @Override
    public void destroy() {}
}
